# -*- coding: UTF-8 -*-
from django.shortcuts import render_to_response
from easycity.userpanel.forms import LoginForm, SignupForm, UpdatePswdForm
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from easycity.utils.CommonUtils import get_base_context_map

"""
def register(request):
    form = UserCreationForm()
    if request.method == 'POST':
        data = request.POST.copy()
        errors = form.get_validation_errors(data)
        if not errors:
            new_user = form.save(data)
            return HttpResponseRedirect("/userpanel/login/")
    else:
        data, errors = {}, {}
        
    return render_to_response('userpanel/signup.html',{'form': form}) 
"""

def signup(request, next_page=None):
    context_map=get_base_context_map(request)
    #context_map = {}
    #context_map['user'] = request.user
    form = SignupForm()
    if request.method == 'POST':
        form = SignupForm(request.POST)
        if form.is_valid():
            user_obj = form.save()
            from django.contrib import auth
            auth.login(request, user_obj)
            return HttpResponseRedirect(next_page or '/')
    context_map['form']=form
    return render_to_response('userpanel/signup.html',context_map)


def login(request):
    #context_map=get_base_context_map(request)
    context_map = {}
    context_map['user'] = request.user
    errorMsg=""
    redirect_to = request.REQUEST.get(REDIRECT_FIELD_NAME, '/')
    form = LoginForm()
    if request.POST:
        errorMsg="登录失败"
        form = LoginForm(request.POST)
        if form.is_valid():
            #做基本验证外的其他验证
            data=form.cleaned_data
            from django.contrib import auth
            user = auth.authenticate(username=data['username'],\
                password=data['password'])
            if user is not None:
                errorMsg=""
                auth.login(request, user)
                #request.session.delete_test_cookie()
                return HttpResponseRedirect(redirect_to)
            else:
                errorMsg="您输入的用户名或密码不正确，请重试。"
    #request.session.set_test_cookie()
    context_map['form']=form
    context_map['errorMsg']=errorMsg
    return render_to_response('userpanel/login.html',context_map)


def logout(request, next_page=None):
    next_page = '/';
    from django.contrib import auth
    auth.logout(request)
    return HttpResponseRedirect(next_page or request.path)


@login_required
def updatepswd(request, next_page=None):
    """
    修改密码
    """
    context_map={}
    context_map['user']=request.user
    next_page = '/'
    u = request.user
    form = UpdatePswdForm()
    if request.POST:
        form = UpdatePswdForm(request.POST, u)
        if form.is_valid():
            u.set_password(form.cleaned_data['new_pswd'])
            u.save()
            return HttpResponseRedirect('/space/')
    context_map['form']=form
    return render_to_response('userpanel/updatepswd.html',context_map)


